package com.yeskery.transfer.config;

import java.io.*;
import java.util.Properties;

/**
 * 数据库配置文件的操作类
 * @author shuangjiang.du
 * @date 2018-12-13 15:52
 * @version 1.0
 */
public class ConfigPropertiesKit {

	/** 服务器主机名 */
	private String serverHost = "server.host";
	/** 服务器端口 */
	private String serverPort = "server.port";
	/** 服务器数据库名 */
	private String serverDatabase = "server.database";
	/** 服务器用户名 */
	private String serverUsername = "server.username";
	/** 服务器密码 */
	private String serverPassword = "server.password";
	/** 客户端主机名 */
	private String clientHost = "client.host";
	/** 客户端端口 */
	private String clientPort = "client.port";
	/** 客户端数据库名 */
	private String clientDatabase = "client.database";
	/** 客户端用户名 */
	private String clientUsername = "client.username";
	/** 客户端密码 */
	private String clientPassword = "client.password";

	/**
	 * 将设置的配置文件信息保存到文件中
	 * @param dataBaseProperties 数据库配置信息
	 * @param path 保存的路径
	 * @throws IOException IOException
	 */
	public void saveConfigProperties(DataBaseProperties dataBaseProperties, String path) throws IOException {
		Properties properties = new Properties();
		DataBaseProperties.TransferProperties serverTransferProperties = dataBaseProperties.getServerTransferProperties();
		DataBaseProperties.TransferProperties clientTransferProperties = dataBaseProperties.getClientTransferProperties();
		properties.setProperty(serverHost, serverTransferProperties.getHost());
		properties.setProperty(serverPort, String.valueOf(serverTransferProperties.getPort()));
		properties.setProperty(serverDatabase, serverTransferProperties.getDatabase());
		properties.setProperty(serverUsername, serverTransferProperties.getUsername());
		properties.setProperty(serverPassword, serverTransferProperties.getPassword());
		properties.setProperty(clientHost, clientTransferProperties.getHost());
		properties.setProperty(clientPort, String.valueOf(clientTransferProperties.getPort()));
		properties.setProperty(clientDatabase, clientTransferProperties.getDatabase());
		properties.setProperty(clientUsername, clientTransferProperties.getUsername());
		properties.setProperty(clientPassword, clientTransferProperties.getPassword());
		properties.store(new FileOutputStream(new File(path)), "store by spring database transfer");
	}

	/**
	 * 从文件读取数据库配置文件信息
	 * @param path 文件的路径
	 * @return 数据库连接信息
	 * @throws IOException IOException
	 */
	public DataBaseProperties loadConfigProperties(String path) throws IOException {
		Properties properties = new Properties();
		properties.load(new FileInputStream(new File(path)));
		DataBaseProperties dataBaseProperties = new DataBaseProperties();
		DataBaseProperties.TransferProperties serverTransferProperties = dataBaseProperties.new TransferProperties();
		DataBaseProperties.TransferProperties clientTransferProperties = dataBaseProperties.new TransferProperties();
		serverTransferProperties.setHost(properties.getProperty(serverHost));
		serverTransferProperties.setPort(Integer.parseInt(properties.getProperty(serverPort)));
		serverTransferProperties.setDatabase(properties.getProperty(serverDatabase));
		serverTransferProperties.setUsername(properties.getProperty(serverUsername));
		serverTransferProperties.setPassword(properties.getProperty(serverPassword));
		clientTransferProperties.setHost(properties.getProperty(clientHost));
		clientTransferProperties.setPort(Integer.parseInt(properties.getProperty(clientPort)));
		clientTransferProperties.setDatabase(properties.getProperty(clientDatabase));
		clientTransferProperties.setUsername(properties.getProperty(clientUsername));
		clientTransferProperties.setPassword(properties.getProperty(clientPassword));
		dataBaseProperties.setServerTransferProperties(serverTransferProperties);
		dataBaseProperties.setClientTransferProperties(clientTransferProperties);
		return dataBaseProperties;
	}
}
